<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <my-box id="box1" class="box1"></my-box>
    <button id="btn">修改class属性</button>
</body>
<script>
    class MyBox extends HTMLElement{
        constructor() {
            super();

            let divnode = document.createElement('div');

            divnode.style.width = '100px';
            divnode.style.height = '100px';
            divnode.style.backgroundColor = '#55a852';

            let shadow = this.attachShadow({
                mode:"open"
            })

            shadow.appendChild(divnode);
        }

        static get observedAttributes() {
            return ['class'];
        }

        connectedCallback() {
            console.log('connected')
        }

        disconnectedCallback() {
            console.log('disconnected')
        }

        attributeChangedCallback(att,oldAtt,newAtt) {
            console.log('attributeChanged')
            console.log(att,oldAtt,newAtt)
        }   
    }

    customElements.define('my-box',MyBox);

    let btn = document.querySelector('#btn');

    btn.onclick = function() {
        let box1 = document.querySelector('#box1');
        box1.setAttribute('class','box');
    }
</script>
</html>